package com.gitee.flwwsg.rpc.client;

import com.gitee.flwwsg.rcp.service.AdditionService;
import com.gitee.flwwsg.rcp.service.HelloService;
import com.gitee.flwwsg.rpc.NameService;
import com.gitee.flwwsg.rpc.RPCAccessPoint;
import com.gitee.flwwsg.rpc.spi.ServiceSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.net.URI;

/**
 * simple rpc 客户端
 */
public class Client {
    private static final Logger logger = LoggerFactory.getLogger(Client.class);

    public static void main(String[] args) throws IOException {
        String serviceName = HelloService.class.getCanonicalName();
        String additionServiceName = AdditionService.class.getCanonicalName();
        //临时目录
        File tmpDirFile = new File(System.getProperty("java.io.tmpdir"));
        File file = new File(tmpDirFile, "simple_rpc_name_service.data");
        String name = "Master MQ";
        try (RPCAccessPoint rpcAccessPoint = ServiceSupport.load(RPCAccessPoint.class)) {
            NameService nameService = rpcAccessPoint.getNameService(file.toURI());
            assert nameService != null;
            URI uri;
            //uri = nameService.lookupService(serviceName);
            //logger.info("找到服务{}， 提供： {}。",serviceName, uri);
            //// service stub
            //HelloService helloService = rpcAccessPoint.getRemoteService(uri, HelloService.class);
            //logger.info("请求服务， name: {}...", name);
            //String response = helloService.hello(name);
            //logger.info("收到响应：{}。", response);
            // service stub
            uri = nameService.lookupService(additionServiceName);
            AdditionService additionService = rpcAccessPoint.getRemoteService(uri, AdditionService.class);
            logger.info("请求服务， name: {}...", additionServiceName);
            Integer res = additionService.add(2);
            logger.info("收到响应：{}。", res);
        }
    }
}
